<template>
  <view class="register-container">
    <!-- 装饰背景气泡 -->
    <view class="bubble bubble-1 floatSoft"></view>
    <view class="bubble bubble-2 floatSoft"></view>
    <view class="bubble bubble-3 floatSoft"></view>
    <view class="bubble bubble-4 floatSoft"></view>
    
    <view class="register-form">
      <view class="input-group">
        <text class="label">邮箱地址</text>
        <input type="email" v-model="form.email" placeholder="请输入邮箱地址" />
      </view>
      
      <view class="input-group">
        <text class="label">用户名</text>
        <input type="text" v-model="form.username" placeholder="请设置用户名" />
      </view>
      
      <view class="input-group">
        <text class="label">真实姓名</text>
        <input type="text" v-model="form.realName" placeholder="请输入真实姓名" />
      </view>
      
      <view class="input-group">
        <text class="label">设置密码</text>
        <input type="password" v-model="form.password" placeholder="请设置6-20位密码" />
        <view class="password-strength" v-if="form.password">
          <view class="strength-bar">
            <view class="strength-item" :class="['level-1', strength >= 1 ? 'active' : '']"></view>
            <view class="strength-item" :class="['level-2', strength >= 3 ? 'active' : '']"></view>
            <view class="strength-item" :class="['level-3', strength >= 5 ? 'active' : '']"></view>
          </view>
          <text class="strength-text">{{ strengthMessage }}</text>
        </view>
      </view>
      
      <view class="input-group">
        <text class="label">确认密码</text>
        <input type="password" v-model="form.confirmPassword" placeholder="请再次输入密码" />
      </view>
      
      <button class="register-btn" @click="register" :disabled="!canRegister" type="primary" hover-class="btn-hover">注册</button>
    
    <!-- 返回登录按钮 -->
    <button class="back-btn" @click="navigateUtil.back()" type="default">返回登录</button>
      
      <view class="agreement">
        <text>注册即代表您同意</text>
        <text class="link" @click="showAgreement('user')">《用户协议》</text>
        <text>和</text>
        <text class="link" @click="showAgreement('privacy')">《隐私政策》</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { api } from '@/api';
import { validateUtil, navigateUtil } from '@/utils';

const form = ref({
  email: '',
  username: '',
  realName: '',
  password: '',
  confirmPassword: ''
});
const strength = ref(0);
const strengthMessage = ref('');

// 计算是否可以注册 - 验证邮箱、用户名、真实姓名和密码
const canRegister = computed(() => {
  if (!form.value.email || !form.value.username || !form.value.realName || !form.value.password || !form.value.confirmPassword) {
    return false;
  }
  
  // 简单的邮箱格式验证
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (!emailRegex.test(form.value.email)) {
    return false;
  }
  
  if (form.value.password !== form.value.confirmPassword) {
    return false;
  }
  
  return true;
});

// 监听密码变化，更新密码强度
watch(() => form.value.password, (newVal) => {
  if (newVal) {
    const result = validateUtil.checkPasswordStrength(newVal);
    strength.value = result.strength;
    strengthMessage.value = result.message;
  } else {
    strength.value = 0;
    strengthMessage.value = '';
  }
});

// 验证邮箱格式
const validateEmail = () => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (!emailRegex.test(form.value.email)) {
    uni.showToast({ title: '请输入正确的邮箱地址', icon: 'none' });
    return false;
  }
  return true;
};

// 邮箱注册 - 使用邮箱、用户名、真实姓名和密码进行注册
const register = async () => {
  if (!canRegister.value) {
    return;
  }
  
  // 验证密码是否一致
  if (form.value.password !== form.value.confirmPassword) {
    uni.showToast({ title: '两次输入的密码不一致', icon: 'none' });
    return;
  }
  
  try {
    // 调用注册接口
    const res = await api.auth.register({
      email: form.value.email,
      username: form.value.username,
      realName: form.value.realName,
      password: form.value.password
    });
    
    uni.showToast({ 
      title: '注册成功，正在登录...', 
      icon: 'success',
      duration: 2000
    });
    
    // 注册成功后跳转到登录页
    setTimeout(() => {
      navigateUtil.back();
    }, 2000);
  } catch (error) {
    console.error('注册失败', error);
    uni.showToast({ 
      title: error.response?.data?.message || '注册失败，请重试', 
      icon: 'none' 
    });
  }
};

// 移除了返回按钮和标题

// 显示协议
const showAgreement = (type) => {
  uni.showModal({
    title: type === 'user' ? '用户协议' : '隐私政策',
    content: type === 'user' ? '用户协议内容...' : '隐私政策内容...',
    showCancel: false
  });
};

onMounted(() => {
  // 页面加载时的处理
});
</script>

<style scoped>
.register-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: linear-gradient(135deg, #FFD1DC 0%, #FFB6C1 40%, #FF69B4 100%);
  padding: 40rpx;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

/* 装饰背景气泡 */
.bubble {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.15);
}

.bubble-1 {
  width: 200rpx;
  height: 200rpx;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.bubble-2 {
  width: 150rpx;
  height: 150rpx;
  top: 30%;
  right: 15%;
  animation-delay: 0.5s;
}

.bubble-3 {
  width: 180rpx;
  height: 180rpx;
  bottom: 20%;
  left: 20%;
  animation-delay: 1s;
}

.bubble-4 {
  width: 120rpx;
  height: 120rpx;
  bottom: 10%;
  right: 25%;
  animation-delay: 1.5s;
}

.register-form {
  flex: 1;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 32rpx;
  padding: 50rpx;
  margin-top: 20rpx;
  box-shadow: 0 8rpx 40rpx rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(20rpx);
}

.input-group {
  margin-bottom: 40rpx;
  position: relative;
}

.label {
  display: block;
  font-size: 30rpx;
  color: #888;
  margin-bottom: 15rpx;
  font-weight: 500;
}

input {
  width: 100%;
  height: 96rpx;
  padding: 0 30rpx;
  border: 2rpx solid #FFD1DC;
  border-radius: 24rpx;
  font-size: 28rpx;
  box-sizing: border-box;
  background-color: #fff;
  transition: all 0.3s ease;
}

input:focus {
  border-color: #FF69B4;
  box-shadow: 0 0 0 6rpx rgba(255, 105, 180, 0.1);
}

input::placeholder {
  color: #ccc;
}

/* 密码强度指示器样式 */
.password-strength {
  margin-top: 15rpx;
}

.strength-bar {
  display: flex;
  gap: 12rpx;
  margin-bottom: 8rpx;
}

.strength-item {
  flex: 1;
  height: 10rpx;
  border-radius: 5rpx;
  background-color: #f5f5f5;
  transition: all 0.3s ease;
}

.strength-item.level-1.active {
  background-color: #FF69B4;
  transform: scaleY(1.2);
}

.strength-item.level-2.active {
  background-color: #FF85A2;
  transform: scaleY(1.2);
}

.strength-item.level-3.active {
  background-color: #FF9EBB;
  transform: scaleY(1.2);
}

.strength-text {
  font-size: 26rpx;
  color: #FF69B4;
  font-weight: 500;
}

.register-btn {
  background: linear-gradient(135deg, #FF69B4 0%, #FF85A2 100%);
  color: #fff;
  border-radius: 48rpx;
  height: 104rpx;
  line-height: 104rpx;
  font-size: 34rpx;
  margin-top: 50rpx;
  font-weight: 500;
  border: none;
  box-shadow: 0 6rpx 20rpx rgba(255, 105, 180, 0.3);
  transition: all 0.3s ease;
}

.register-btn:active {
  transform: scale(0.98);
  box-shadow: 0 4rpx 15rpx rgba(255, 105, 180, 0.2);
}

.register-btn:disabled {
  background: linear-gradient(135deg, #FFD1DC 0%, #FFB6C1 100%);
  box-shadow: none;
}

.back-btn {
  background-color: transparent;
  color: #FF69B4;
  border-radius: 48rpx;
  height: 96rpx;
  line-height: 96rpx;
  font-size: 32rpx;
  margin-top: 30rpx;
  border: 2rpx solid #FF69B4;
  transition: all 0.3s ease;
}

.back-btn:active {
  background-color: rgba(255, 105, 180, 0.1);
}

.agreement {
  margin-top: 30rpx;
  text-align: center;
  font-size: 24rpx;
  color: #999;
  line-height: 1.5;
}

.link {
  color: #FF69B4;
  text-decoration: underline;
}

/* 浮动动画 */
@keyframes floatSoft {
  0%, 100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20rpx) scale(1.05);
  }
}

.floatSoft {
  animation: floatSoft 6s ease-in-out infinite;
}

/* 按钮悬停效果 */
.btn-hover {
  transform: translateY(-4rpx);
  box-shadow: 0 10rpx 25rpx rgba(255, 105, 180, 0.4);
}
</style>